package com.xmx.productcenter.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmx.common.utils.PageUtils;
import com.xmx.common.utils.Query;

import com.xmx.productcenter.dao.GoodsDao;
import com.xmx.productcenter.entity.GoodsEntity;
import com.xmx.productcenter.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    GoodsDao goodsDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<GoodsEntity> queryByCategory(List<Integer> l2List, int offset, int limit) {
        return goodsDao.selectPage(new Page(offset, limit), Wrappers.<GoodsEntity>lambdaQuery().
                in(GoodsEntity::getCategoryId, l2List).
                eq(GoodsEntity::getIsOnSale, true).
                eq(GoodsEntity::getDeleted, false).
                orderByDesc(GoodsEntity::getAddTime)).getRecords();
    }

    @Override
    public IPage<GoodsEntity> related(Page<GoodsEntity> page, Integer id) {
        return goodsDao.related(page, id);
    }

}